//
//  BookManager.m
//  Homework_03 - NSString NSArray NSMutableArray
//
//  Created by 岁变 on 16/7/6.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "BookManager.h"
#import "Book.h"

@interface BookManager ()
@property (nonatomic, retain) NSMutableArray *mArr;
@end

@implementation BookManager

- (instancetype)init {
    self = [super init];
    if (self) {
        _mArr = [NSMutableArray array];
    }
    return self;
}

- (void)addBook:(Book *)book {
    [self.mArr addObject:book];
    NSLog(@"添加成功");
}

//- (void)deleteBook:(NSString *)name {
//    if (self.mArr != nil) {
//        for (Book *book in self.mArr) {
//            if ([book.name isEqualToString:name]) {
//                [self.mArr removeObject:book];
//            }
//        }
//
//    }
//}

- (void)deleteBook:(NSString *)name {
    NSMutableArray *bookName = [NSMutableArray array];
    NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
    NSInteger a = 0;
    for (Book *book in self.mArr) {
        [bookName addObject:book.name];
        if ([book.name isEqualToString:name]) {
            [indexSet addIndex:a];
        }
        a++;
    }
    BOOL isHaveBook = [bookName containsObject:name];
    if (isHaveBook) {
        [self.mArr removeObjectsAtIndexes:indexSet];
        NSLog(@"删除成功");
    } else {
        NSLog(@"该书不存在");
    }
}

- (void)showAllBook {
    for (Book *book in self.mArr) {
        NSLog(@"name:%@  price:%@", book.name, book.price);
    }
}

- (void)modifiedPrice:(NSString *)name price:(NSString *)price {
    for (Book *book in self.mArr) {
                if ([book.name isEqualToString:name]) {
            book.price = price;
        }
    }
}
@end
